package com.example.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.example.entity.Roles;
import com.example.entity.Students;
import com.example.entity.StudentsVO;
import com.example.entity.User;
import com.example.entity.UserVO;
import com.example.service.UserService;

@RequestMapping("/user")
@RestController
public class UserController {
	
	@Autowired
	private UserService userService;
	/**
	 * 登录
	 * @param LoginName
	 * @param Password
	 * @return
	 */
	@RequestMapping("/loginNameAndPwd")
	public ModelAndView loginNameAndPwd(String LoginName, String Password) {
		User user = null;
		ModelAndView modelAndView = new ModelAndView();
		user = userService.selectByName(LoginName);
		if (user != null) {
			user = userService.selectByNameAndPwd(LoginName, Password);
			if (user != null) {
				modelAndView.setViewName("redirect:/main.html");
		        return modelAndView;

			}
			modelAndView.setViewName("redirect:/login.html");
	        return modelAndView;

		}
		modelAndView.setViewName("redirect:/login.html");
        return modelAndView;
	}
	
//	@RequestMapping("/GetUsersLayUI")
//	public ResponeMessageUtil GetUsersLayUI(Integer limit,Integer page,String loginName, String createTime, String endDate, String isLockout, String orderBy) {
//		Integer setValue = PageUtil.getSetValue(page, limit);
//		return ResponeMessageUtil.ok(userService.getCount(loginName, createTime, endDate, isLockout, orderBy), userService.selectAll(setValue, limit, loginName, createTime, endDate, isLockout, orderBy));
//	}
	/**
	 * 多条件查询
	 * @param userVO
	 * @return
	 */
	@RequestMapping("/GetUsersLayUI")
	public Map<String, Object> GetUsersLayUI(UserVO userVO){
		Map<String, Object> reMap=new HashMap<String, Object>();
		userVO.setOffset((userVO.getPage()-1)*userVO.getLimit());
		List<User> list=userService.GetUsersLayUI(userVO);
		int count=userService.getCount(userVO);
		reMap.put("code","0");
		reMap.put("msg", "success");
		reMap.put("count", count);
		reMap.put("data", list);
		return reMap;
	}
	
	/**
	 * 新增用户
	 * @param id
	 * @return
	 */

	@RequestMapping(path = "/addUser")
	public Map<String, Object> addUser(User user) { // 受影响的行数 
		int rows = userService.addUser(user);
		Map<String, Object> rsMap =new HashMap<String, Object>();
		rsMap.put("code", rows > 0 ? "0" : "1");
		rsMap.put("msg", rows > 0 ? "添加成功" : "添加失败");
		return rsMap;

	}
	
	/**
	 * 修改用户
	 * @param id
	 * @return
	 */

	@RequestMapping(path = "/upUser")
	public Map<String, Object> upUser(User user) { // 受影响的行数 
		int rows = userService.upUser(user);
		Map<String, Object> rsMap =new HashMap<String, Object>();
		rsMap.put("code", rows > 0 ? "0" : "1");
		rsMap.put("msg", rows > 0 ? "修改成功" : "修改失败");
		return rsMap;

	}
	
	/**
	 * 根据id删除用户
	 * @param id
	 * @return
	 */
	@PostMapping("/delUser")
	@ResponseBody
	public Map<String, Object> delUser(int id){
		Map<String, Object> relMap=new HashMap<String, Object>();
		int rows=userService.delUser(id);
		relMap.put("code",rows>0? "0":"1");
		relMap.put("msg",rows>0? "删除成功":"删除失败");
		return relMap;
	}
	/**
	 * 锁定
	 * @param id
	 * @return
	 */

	@PostMapping(path = "/isLock")
	public Map<String, Object> isLock(int id) { // 受影响的行数 
		int rows = userService.islock(id);
		Map<String, Object> rsMap =new HashMap<String, Object>();
		rsMap.put("code", rows > 0 ? "0" : "1");
		rsMap.put("msg", rows > 0 ? "锁定成功" : "锁定失败");
		return rsMap;

	}
	/**
	 * 解锁
	 * @param id
	 * @return
	 */

	@PostMapping(path = "/noIsLock")
	public Map<String, Object> noIsLock(int id) { // 受影响的行数 
		int rows = userService.noIslock(id);
		Map<String, Object> rsMap =new HashMap<String, Object>();
		rsMap.put("code", rows > 0 ? "0" : "1");
		rsMap.put("msg", rows > 0 ? "解锁成功" : "解锁失败");
		return rsMap;

	}
	
	/**
	 * 重置密码
	 * @param id
	 * @return
	 */
	@PostMapping("/ResetPassword")
	@ResponseBody
	public Map<String, Object> ResetPassword(String id){
		Map<String, Object> relMap=new HashMap<String, Object>();
		int rows=userService.ResetPassword(id);
		relMap.put("code",rows>0? "0":"1");
		relMap.put("msg",rows>0? "重置成功":"重置失败");
		return relMap;
	}
	/**
	 *根据用户id获取角色
	 * @param id
	 * @return
	 */
	@PostMapping(path = "/getRolesByUid")
	public Map<String, Object> getRolesByUid(String id) { // 受影响的行数
		
		List<Roles> list = userService.getRolesByUid(id);
		Map<String, Object> rsMap =new HashMap<String, Object>();
		rsMap.put("code", "code");
		rsMap.put("msg","");
		rsMap.put("count", list.size());
		rsMap.put("data",list);
		return rsMap;

	}
	/**
	 * 查询所有角色
	 * @param id
	 * @return
	 */
	@PostMapping(path = "/GetRolesAll")
	public Map<String, Object> GetRolesAll() { // 受影响的行数
		
		List<Roles> list = userService.GetRolesAll();
		Map<String, Object> rsMap =new HashMap<String, Object>();
		rsMap.put("code", "code");
		rsMap.put("msg","");
		rsMap.put("count", list.size());
		rsMap.put("data",list);
		return rsMap;

	}
	/**
	 * 网络学生的多条件查询
	 * @param StudentsVO
	 * @return
	 */
	@RequestMapping("/findStudents")
	public Map<String, Object> findStudents(StudentsVO studentsVO){
		Map<String, Object> reMap=new HashMap<String, Object>();
		studentsVO.setOffset((studentsVO.getPage()-1)*studentsVO.getLimit());
		List<Students> list=userService.findStudents(studentsVO);
		int count=userService.getCountStudents(studentsVO);
		reMap.put("code","0");
		reMap.put("msg", "success");
		reMap.put("count", count);
		reMap.put("data", list);
		return reMap;
	}
	
	/**
	 * 新增网络学生
	 * @param students
	 * @return
	 */

	@RequestMapping(path = "/addStudents")
	public Map<String, Object> addStudents(Students students) { // 受影响的行数 
		int rows = userService.addStudents(students);
		Map<String, Object> rsMap =new HashMap<String, Object>();
		rsMap.put("code", rows > 0 ? "0" : "1");
		rsMap.put("msg", rows > 0 ? "添加成功" : "添加失败");
		return rsMap;

	}

}